﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eCommerce.Core.Domain.Products;
using eCommerce.Services.Pictures;
using eCommerce.Services.Products;

namespace eCommerce.Controllers
{
    public class ItemsController : Controller
    {
        private IItemService _itemService;
        private IPictureService _pictureService;

        public ItemsController(IItemService itemService, IPictureService pictureService)
        {
            _itemService = itemService;
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowItem(string itemGroupNo)
        {
            var itemGroup = _itemService.GetItemGroupByNo(itemGroupNo);

            var product = itemGroup.Product;



            var items = itemGroup.Items;

            var showItemModel = new ShowItemModel()
            {
                ProductAttributeModels = this.BuildShowProductAttributeModels(product.ProductAttributeValues, itemGroupNo),
                ItemAttributeModels = this.BuildShowItemAttributeModels(items),
                UseItemAttribute = product.UseItemAttribute,
                UseProductAttribute = product.UseProductAttribute,
            };

            
            return View("showitem",showItemModel);
        }

        private IList<ShowProductAttributeModel> BuildShowProductAttributeModels(
            IList<ProductAttributeValue> productAttributeValues, string itemGroupNo)
        {
            var result = new List<ShowProductAttributeModel>();
            foreach (var c in productAttributeValues)
            {
                var itemGroupIconId = ItemGroupIconPictureId(c.ToItemGroupNo());

                var model = new ShowProductAttributeModel()
                {
                    AttributeValue = c.Value,
                    IsChoosen = itemGroupNo == c.ToItemGroupNo(),
                    ItemUrl = string.Format("/items/showitem?itemGroupNo={0}", c.ToItemGroupNo()),
                    IconUrl = itemGroupIconId.HasValue? _pictureService.GetPictureUrl(itemGroupIconId.Value,25,false): string.Empty,
                };
                result.Add(model);
            }
            return result;
        }


        private int? ItemGroupIconPictureId(string itemGroupNo)
        {
            var itemGroup = _itemService.GetItemGroupByNo(itemGroupNo);
            return itemGroup.IconPictureId;
        }
        private IList<ShowItemAttributeModel> BuildShowItemAttributeModels(
            IList<Item> items)
        {
            return items.Where(c=>c.StockQuantity>0).Select(c => new ShowItemAttributeModel()
            {
                AttributeValue = c.ItemAttributeValue.Value,
              
                ItemCode = c.Id.ToString(),
            }).ToList();
        }

    }

    public class ShowItemModel
    {


        public bool UseProductAttribute { get; set; }
        public bool UseItemAttribute { get; set; }

        public string ItemName { get; set; }

        public string PicUrl { get; set; }

        public IList<ShowProductAttributeModel> ProductAttributeModels { get; set; }

        public IList<ShowItemAttributeModel> ItemAttributeModels { get; set; }

    }

    public class ShowProductAttributeModel
    {
        public string AttributeValue { get;set; }
        public string IconUrl { get; set; }
        public string ItemUrl { get; set; }

        public bool IsChoosen{ get; set; }
    }

    public class ShowItemAttributeModel
    {
        public string AttributeValue { get; set; }
        public string ItemCode { get; set; }
             
    }

}